package rsj.monitor;

import com.alibaba.fastjson.JSONObject;
import com.rlyy.aibase.utils.base.Box;
import com.rlyy.aibase.utils.base.MapNode;
import common.util.BoxUtil;
import common.util.DateTimeFormatterUtil;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rsj.api.user.BaseLink;
import rsj.entity.Job;
import rsj.entity.Node;
import rsj.utils.RedisUtils;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author          xuguoliang@rlyy.com
 * @date            2019/12/27 9:21
 * @description     系统运行监控接口
 * @package         rsj.monitor.MonitorInfoCheck
 */
@RestController
public class JobInfoCheck extends BaseLink {

    private static final long BILL_ALERT = 90;
    private static final long ORGP_ALERT = 90;
    private static final long PROD_ALERT = 60 * 60 *36;

    @RequestMapping(value = "rsj_monitor_job_info")
    public Box executeLink(Box in){
        MapNode data = new MapNode();
        String billTime = RedisUtils.getString("rsj|job|bill");
        String orgpTime = RedisUtils.getString("rsj|job|orgp_status");
        String prodStatus = RedisUtils.getString("rsj|job|prod_usage");
        String remindTime = RedisUtils.getString("rsj|job|remind");
        Map<Object,Object> billHealth = RedisUtils.hmget("rsj|job|bill_health");
        Map<Object,Object> orgpHealth = RedisUtils.hmget("rsj|job|orgp_health");
        Map<Object,Object> prodHealth = RedisUtils.hmget("rsj|job|prod_health");
        Map<Object,Object> remindHealth = RedisUtils.hmget("rsj|job|remind_health");
        List<Job> jobList = new ArrayList<>();
        Job orgp = Job.builder().jobName("合同任务")
                .jobStatus(isOk(orgpTime,ORGP_ALERT))
                .jobRuntime(orgpTime).nodes(map2List(orgpHealth, ORGP_ALERT)).build();
        Job bill = Job.builder().jobName("账单任务")
                .jobStatus(isOk(billTime,BILL_ALERT))
                .jobRuntime(billTime).nodes(map2List(billHealth, BILL_ALERT)).build();
        Job prod = Job.builder().jobName("产品使用统计任务")
                .jobStatus( isOk(prodStatus,PROD_ALERT))
                .jobRuntime(prodStatus).nodes(map2List(prodHealth, PROD_ALERT)).build();
        Job remind = Job.builder().jobName("预警任务")
                .jobStatus( isOk(remindTime,PROD_ALERT))
                .jobRuntime(remindTime).nodes(map2List(remindHealth, PROD_ALERT)).build();
        jobList.add(orgp);
        jobList.add(bill);
        jobList.add(prod);
        jobList.add(remind);
        data.putNode("jobs", JSONObject.toJSONString(jobList));
        return BoxUtil.successBoxToData(in,"ok",data);
    }

    private List<Node> map2List(Map map, long alertSeconds){
        List<Node> list = new ArrayList<>();
        if(CollectionUtils.isEmpty(map)){
            return list;
        }
        map.forEach((k,v)->{
            Node node = new Node();
            node.setNodeId((String) k);
            node.setNodeRuntime((String) v);
            node.setNodeStatus(isOk((String) v,alertSeconds));
            list.add(node);
        });
        return list;
    }

    private boolean isOk(String time, long seconds){
        if(StringUtils.isEmpty(time)){
            return false;
        }
        LocalDateTime dateTime = DateTimeFormatterUtil.stringDateToLocalDateTime(time);
        Duration between = Duration.between(dateTime, LocalDateTime.now());
        return between.getSeconds() < seconds;
    }

}
